<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { userApi } from '../services/api';

const router = useRouter();
const loading = ref(false);
const formData = reactive({
  username: 'admin',
  password: '123456'
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
};

const loginFormRef = ref(null);

const handleLogin = async () => {
  // 验证表单
  loginFormRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }

    loading.value = true;
    try {
      // 调用登录接口
      const response = await userApi.login(formData);
      console.log('登录响应:', response);
      
      // 存储用户信息和token到本地
      localStorage.setItem('user', JSON.stringify(response));
      // 确保token正确存储，适用于不同的后端返回格式
      if (response.token) {
        localStorage.setItem('token', response.token);
      } else if (response.data && response.data.token) {
        localStorage.setItem('token', response.data.token);
      }
      
      // 显示登录成功消息
      ElMessage.success('登录成功');
      
      // 跳转到订单列表页面
      router.push('/orders');
    } catch (error) {
      ElMessage.error(error.message || '登录失败，请检查用户名和密码');
    } finally {
      loading.value = false;
    }
  });
};
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">下单小票打印管理系统</h2>
      <el-form
        ref="loginFormRef"
        :model="formData"
        :rules="rules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
            autocomplete="username"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            clearable
            show-password
            autocomplete="current-password"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            @click="handleLogin"
            :loading="loading"
            style="width: 100%"
            size="large"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
}

.login-form {
  margin-bottom: 20px;
}

.login-info {
  text-align: center;
  font-size: 14px;
  color: #909399;
}

.login-info p {
  margin: 5px 0;
}

/* 调整SVG图标大小 */
:deep(.el-input__prefix-inner svg) {
  width: 18px;
  height: 18px;
}

:deep(.el-icon svg) {
  width: 18px;
  height: 18px;
}
</style>